import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.util.ArrayList;

/**
 * 我方牌库
 */
public class MyLibrary extends JPanel{
    /**
     * 选定的牌
     */
    static Dominoes SelectedDominoes = null;
    /**骨牌数量*/
    int DominoCount = 7;
    /**
     * 我方牌库实例
     */
    public  static MyLibrary Instance = new MyLibrary();
    /**
     * 我方全部骨牌集合
     */
    ArrayList<Dominoes> AllMyDominoes = new ArrayList<>();
    /**
     * 布局
     */
    FlowLayout f = new FlowLayout(FlowLayout.CENTER);

    /**
     * 初始化
     */
    public void Init(){
        DominoCount = 7;
       // Border blackLine = BorderFactory.createLineBorder(Color.BLACK);
        //Instance.setSize(800,200);
        WidthUpdate();
        Instance.setVisible(true);
      //  Instance.setBorder(blackLine);
        //GridLayout gridLayout = new GridLayout();
        f.setHgap(26);
        f.setVgap(30);
        Instance.setLayout(f);
        for(int i = 0;i < DominoCount;i++){
            Dominoes tmp = DominoManager.Instance.GetADomino();
            tmp.addMouseListener(tmp);
            AllMyDominoes.add(tmp);
            this.add(tmp);
        }
    }

    /**
     * 更新我方手牌显示区域宽度
     */
    public void WidthUpdate(){
        int width = DominoCount * 75 + (DominoCount-1)*46;
        Instance.setSize(width,200);
    }

    /**
     * 更新我方牌库显示
     */
    public void Update(){
        DominoCount--;
        WidthUpdate();
        AllMyDominoes.remove(SelectedDominoes);
        remove(SelectedDominoes);
        SelectedDominoes = null;
        repaint();
    }

    /**
     * 计算我方总分
     * @return 我方总分
     */
    public int CalePoints(){
        int ans = 0;
        for(Dominoes tmp:AllMyDominoes){
           ans += tmp.point[0] + tmp.point[1];
        }
        return ans;
    }
}
